<?php
App::uses('AdminController', 'Controller');
/**
 * Images Controller
 *
 * @property Image $Image
 * @property RequestHandlerComponent $RequestHandler
 */
class ImagesController extends AdminController {

/**
 * Helpers
 *
 * @var array
 */
    public $helpers = array(
        'Js', 
        'Fancybox.Fancybox',
        'Form' => array(
            'className' => 'BootstrapForm'
        ),
        'Html',
        'Session' => array(
            'className' => 'BootstrapSession'
        ),
        'Paginator' => array(
            'className' => 'BootstrapPaginator'
        )      
    );

/**
 * Components
 *
 * @var array
 */
    public $components = array('RequestHandler', 'Auth', 'Session');

/**
 * index method
 *
 * @return void
 */
    public function index() {
        $this->Image->recursive = 0;
        $this->set('images', $this->paginate());
    }

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
    public function view($id = null) {
        $this->Image->id = $id;
        if (!$this->Image->exists()) {
            throw new NotFoundException(__('Invalid image'));
        }
        $this->set('image', $this->Image->read(null, $id));
    }

/**
 * admin_index method
 *
 * @return void
 */
    public function admin_index() {
        $this->Image->recursive = 0;
        $this->set('images', $this->paginate());
    }

/**
 * admin_view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
    public function admin_view($id = null) {
        $this->Image->id = $id;
        if (!$this->Image->exists()) {
            throw new NotFoundException(__('Invalid image'));
        }
        $this->set('image', $this->Image->read(null, $id));
    }

/**
 * admin_add method
 *
 * @return void
 */
    public function admin_add() {
        if ($this->request->is('post')) {
            $this->Image->create();
            if ($this->Image->save($this->request->data)) {
                $this->Session->setFlash(__('The image has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The image could not be saved. Please, try again.'));
            }
        }
        $products = $this->Image->Product->find('list');
        $this->set(compact('products'));
    }

/**
 * admin_edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
    public function admin_edit($id = null) {
        $this->Image->id = $id;
        if (!$this->Image->exists()) {
            throw new NotFoundException(__('Invalid image'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Image->save($this->request->data)) {
                $this->Session->setFlash(__('The image has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The image could not be saved. Please, try again.'));
            }
        } else {
            $this->request->data = $this->Image->read(null, $id);
        }
        $products = $this->Image->Product->find('list');
        $this->set(compact('products'));
    }

/**
 * admin_delete method
 *
 * @throws MethodNotAllowedException
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
    public function admin_delete($id = null) {
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->Image->id = $id;
        if (!$this->Image->exists()) {
            throw new NotFoundException(__('Invalid image'));
        }
        if ($this->Image->delete()) {
            $this->Session->setFlash(__('Image deleted'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Image was not deleted'));
        $this->redirect(array('action' => 'index'));
    }
}
